namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineSineLine : PaintLineBase
    {
        public PaintLineSineLine()
        {
            base.LineType = PaintLineType.TimeRuler;
            base.PlName_zh = "正弦线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num4 = base.CgInstance.GetX(num2);
                PaintLineBase.PointValue value4 = base.PointList[0];
                float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
                PaintLineBase.PointValue value5 = base.PointList[1];
                float num6 = base.CgInstance.GetY(base.OwnedPanelID, value5.Value, null);
                if (((crossHairPoint.X >= (x - (2.5f * base.LineWidth))) && (crossHairPoint.X <= (x + (2.5f * base.LineWidth)))) && ((crossHairPoint.Y >= (num5 - (2.5f * base.LineWidth))) && (crossHairPoint.Y <= (num5 + (2.5f * base.LineWidth)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeNS;
                    base.MoveType = 1;
                }
                else if (((crossHairPoint.X >= (num4 - (2.5f * base.LineWidth))) && (crossHairPoint.X <= (num4 + (2.5f * base.LineWidth)))) && ((crossHairPoint.Y >= (num6 - (2.5f * base.LineWidth))) && (crossHairPoint.Y <= (num6 + (2.5f * base.LineWidth)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeWE;
                    base.MoveType = 2;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                PaintLineBase.PointValue value2 = pList[0];
                double chartValue = value2.Value;
                PaintLineBase.PointValue value3 = pList[1];
                double num2 = value3.Value;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float num5 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num6 = base.CgInstance.GetX(num4) - base.CgInstance.LeftYScaleWidth;
                float num7 = (base.CgInstance.GetY(base.OwnedPanelID, chartValue, null) - div.Rect.Y) - div.TitleHeight;
                float num8 = (base.CgInstance.GetY(base.OwnedPanelID, num2, null) - div.Rect.Y) - div.TitleHeight;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                List<PointF> list = new List<PointF>();
                double num9 = 6.2831853071795862 / ((double) ((num6 - num5) * 4f));
                if (num5 != num6)
                {
                    for (float i = -num5; i <= (base.CgInstance.GetWorkSpaceX() - num5); i++)
                    {
                        float num11 = (float) (((0.5 * (num8 - num7)) * Math.Sin(i * num9)) * 2.0);
                        list.Add(new PointF(i + num5, num11 + num7));
                    }
                    PointF[] points = list.ToArray();
                    g.DrawCurve(pen, points);
                }
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num5 - (base.LineWidth * 2.5f), num7 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num6 - (base.LineWidth * 2.5f), num8 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                brush.Dispose();
                pen.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                if ((mouseOverIndex >= 0) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                {
                    int num3 = mouseOverIndex;
                    int num4 = num3 - (base.CgInstance.GetMaxVisibleRecord() / 10);
                    if ((num4 >= 0) && (num3 != num4))
                    {
                        ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                        DataRow row = base.CgInstance.DataSource.Rows[num4];
                        DataRow row2 = base.CgInstance.DataSource.Rows[num3];
                        string key = row[base.CgInstance.CalenderScaleField].ToString();
                        string str2 = row2[base.CgInstance.CalenderScaleField].ToString();
                        base.PointList.Clear();
                        double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                        base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint + ((div.MaxValue[0] - div.MinValue[0]) / 4.0)));
                        base.PointList.Add(new PaintLineBase.PointValue(1, str2, valueByPoint));
                        return true;
                    }
                }
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                PaintLineBase.PointValue value2 = base.PointList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                RectangleF ef = new RectangleF(x - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint) || ef2.Contains((PointF) crossHairPoint))
                {
                    return true;
                }
                double num7 = 6.2831853071795862 / ((double) ((num6 - x) * 4f));
                PointF empty = PointF.Empty;
                for (float i = -x; i <= (base.CgInstance.GetWorkSpaceX() - x); i++)
                {
                    float num9 = (float) (((0.5 * (num2 - num)) * Math.Sin(i * num7)) * 2.0);
                    PointF tf2 = new PointF(i + x, num9 + num);
                    if (i == -x)
                    {
                        if (((crossHairPoint.X >= (tf2.X - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (tf2.X + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (tf2.Y - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (tf2.Y + (base.LineWidth * 2.5f)))))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        RectangleF ef3 = new RectangleF(empty.X - 2f, (empty.Y <= tf2.Y) ? empty.Y : (tf2.Y - 2f), Math.Abs((float) (tf2.X - empty.X)) + 4f, Math.Abs((float) (tf2.Y - empty.Y)) + 4f);
                        if (ef3.Contains((PointF) crossHairPoint))
                        {
                            return true;
                        }
                    }
                    empty = tf2;
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    PaintLineBase.PointValue value2 = base.PointList[0];
                    int num3 = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                    PaintLineBase.PointValue value3 = base.PointList[1];
                    int num4 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num5 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num6 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num7 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num8 = (num5 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num7 - num6);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num10 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            int num11 = (base.CgInstance.GetRecordByTime(value4.Key) + (num10 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            int num12 = (base.CgInstance.GetRecordByTime(value5.Key) + (num10 - indexByPoint)) - 1;
                            if (num11 >= 0)
                            {
                                if (num11 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num11 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num11 = 0;
                            }
                            if (num12 < 0)
                            {
                                num12 = 0;
                            }
                            else if (num12 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num12 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            if (num12 != num11)
                            {
                                PaintLineBase.PointValue value6 = base.BeginMoveValue[0];
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num11][base.CgInstance.CalenderScaleField].ToString(), value6.Value + num8);
                                PaintLineBase.PointValue value7 = base.BeginMoveValue[1];
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num12][base.CgInstance.CalenderScaleField].ToString(), value7.Value + num8);
                            }
                            return;
                        }
                        case 1:
                            if (mouseOverIndex < num4)
                            {
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;

                        case 2:
                            if (mouseOverIndex > num3)
                            {
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;
                    }
                }
            }
        }
    }
}

